package com.bjgov.scoresys.action;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bjgov.scoresys.entity.User;
import com.bjgov.scoresys.form.UserForm;
import com.bjgov.scoresys.resource.UserKey;
import com.bjgov.scoresys.service.UserHandler;

public class UserAction extends MappingDispatchAction{
	
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		User user = UserHandler.getUser(userForm.getUserName());
		if(user!=null) {
			if(userForm.getPasswd().equals(user.getPassword())) {
				if(user.getStatus()==UserKey.State_Normal) {
					user.setLastLoginDate(new Date());
					user.setLoginCount(user.getLoginCount()+1);
					user.setLastLoginIp(request.getRemoteAddr());
					request.getSession().setAttribute("user", user);
					
					
					
					return mapping.findForward("success");	
				}else{
					request.setAttribute("message", "登陆失败，用户已经被管理员锁定");
				}
			}else{
				request.setAttribute("message", "登陆失败，密码错误");
			}
		}else{
			request.setAttribute("message", "登陆失败，用户不存在");
		}
		request.setAttribute("returnURL", mapping.getInput());		
		return mapping.findForward("fail");
	}
    
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		UserForm userForm = (UserForm) form;
		User user = (User) request.getSession().getAttribute("user");
		if(user.getId()==userForm.getId() || user.getAuthority()==UserKey.Authority_Admin) {
			//管理员或者自己可以修改用户信息
			user =  UserHandler.getUser(userForm.getId());
			user.setAuthority(userForm.getAuthority());
			user.setLoginName(userForm.getUserName());
			user.setRealName(userForm.getRealName());
			user.setPassword(userForm.getPasswd());
			user.setStatus(userForm.getStatus());
			UserHandler.saveUser(user);
			request.setAttribute("message", "修改用户成功");
			request.setAttribute("nextURL", "/user/editSystemUser.jsp");			
			return mapping.findForward("tip");
		}else{
			request.setAttribute("message", "修改失败，用户没有修改的权限");
			request.setAttribute("returnURL", "/user/editSystemUser.jsp");
			return mapping.findForward("fail");
		}
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.getSession().removeAttribute("user");
		return mapping.findForward("logout");
	}
	
	public ActionForward register(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		User user = UserHandler.getUser(userForm.getUserName());
		if(user!=null) {
			request.setAttribute("message", "注册失败，用户已经存在");
			request.setAttribute("returnURL", mapping.getInput());
			return mapping.findForward("fail");
		}
		User regUser = new User();
		regUser.setLoginName(userForm.getUserName());
		regUser.setRealName(userForm.getRealName());
		regUser.setPassword(userForm.getPasswd());
		regUser.setAuthority(userForm.getAuthority());
		regUser.setLoginCount(0);
		regUser.setCreateDate(new Date());			
		regUser.setStatus(UserKey.State_Normal);
		UserHandler.saveUser(regUser);
		
		request.setAttribute("message", "注册成功");
		request.setAttribute("nextURL", mapping.getInput());
		return mapping.findForward("tip");
	} 
}
